
package org.dyndns.cooland.updater.dialogs;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.EmptyBorder;

import org.dyndns.cooland.updater.db.dao.SitesDAO;
import org.dyndns.cooland.updater.db.model.Sites;
import org.eclipse.wb.swing.FocusTraversalOnArray;

public class AddSiteDlg extends JDialog {
	
	private static final long	serialVersionUID	= -5902031870167057942L;
	private final JPanel		contentPanel		= new JPanel();
	private JTextField			nameField;
	private JLabel				lblName;
	private JTextField			urlField;
	private JButton				okButton;
	private JButton				cancelButton;
	private Sites				bean				= null;
	private SitesDAO			dao					= new SitesDAO();
	private JPanel				buttonPane;
	
	/**
	 * Launch the application.
	 */
	// public static void main(String[] args) {
	// try {
	// AddSiteDlg dialog = new AddSiteDlg();
	// dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
	// dialog.setVisible(true);
	// } catch (Exception e) {
	// e.printStackTrace();
	// }
	// }
	
	/**
	 * Create the dialog.
	 */
	public AddSiteDlg (Sites sitebean) {

		bean = sitebean;
		if (bean == null){
			setTitle("Add site");
		}
		else{
			setTitle("Edit site");
		}
		
		setResizable(false);
		setModal(true);
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		
		setBounds(100, 100, 450, 150);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		{
			lblName = new JLabel("Name :");
		}
		{
			nameField = new JTextField();
			nameField.setColumns(10);
		}
		
		JLabel lblUrl = new JLabel("URL :");
		
		urlField = new JTextField();
		urlField.setColumns(10);
		GroupLayout gl_contentPanel = new GroupLayout(contentPanel);
		gl_contentPanel
				.setHorizontalGroup(gl_contentPanel
						.createParallelGroup(Alignment.LEADING)
						.addGroup(
								gl_contentPanel
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												gl_contentPanel
														.createParallelGroup(
																Alignment.LEADING)
														.addComponent(
																lblName,
																GroupLayout.PREFERRED_SIZE,
																51,
																GroupLayout.PREFERRED_SIZE)
														.addComponent(lblUrl))
										.addPreferredGap(
												ComponentPlacement.RELATED)
										.addGroup(
												gl_contentPanel
														.createParallelGroup(
																Alignment.LEADING,
																false)
														.addComponent(
																nameField,
																GroupLayout.PREFERRED_SIZE,
																359,
																GroupLayout.PREFERRED_SIZE)
														.addComponent(
																urlField,
																GroupLayout.PREFERRED_SIZE,
																359,
																GroupLayout.PREFERRED_SIZE))
										.addContainerGap()));
		gl_contentPanel
				.setVerticalGroup(gl_contentPanel
						.createParallelGroup(Alignment.LEADING)
						.addGroup(
								gl_contentPanel
										.createSequentialGroup()
										.addGroup(
												gl_contentPanel
														.createParallelGroup(
																Alignment.LEADING,
																false)
														.addGroup(
																gl_contentPanel
																		.createSequentialGroup()
																		.addGap(
																				13)
																		.addComponent(
																				lblName,
																				GroupLayout.PREFERRED_SIZE,
																				27,
																				GroupLayout.PREFERRED_SIZE))
														.addGroup(
																gl_contentPanel
																		.createSequentialGroup()
																		.addContainerGap()
																		.addComponent(
																				nameField,
																				GroupLayout.PREFERRED_SIZE,
																				22,
																				GroupLayout.PREFERRED_SIZE)
																		.addPreferredGap(
																				ComponentPlacement.RELATED)
																		.addGroup(
																				gl_contentPanel
																						.createParallelGroup(
																								Alignment.BASELINE,
																								false)
																						.addComponent(
																								urlField,
																								GroupLayout.PREFERRED_SIZE,
																								GroupLayout.DEFAULT_SIZE,
																								GroupLayout.PREFERRED_SIZE)
																						.addComponent(
																								lblUrl))))
										.addGap(23)));
		gl_contentPanel.linkSize(SwingConstants.VERTICAL, new Component[] {
				nameField, urlField });
		gl_contentPanel.linkSize(SwingConstants.HORIZONTAL, new Component[] {
				nameField, urlField });
		contentPanel.setLayout(gl_contentPanel);
		contentPanel.setFocusTraversalPolicy(new FocusTraversalOnArray(
				new Component[] { nameField, urlField, lblName, lblUrl }));
		{
			buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				okButton = new JButton("OK");
				okButton.addActionListener(new ActionListener() {
					
					public void actionPerformed(ActionEvent e) {

						String name = nameField.getText();
						String url = urlField.getText();
						
						if (bean == null){
							// add site
							Sites newbean = new Sites(name, url, null, 1);
							dao.insert(newbean);
						}
						else{
							// System.out.println(bean.getDescription());
							bean.setName(name);
							bean.setUrl(url);
							dao.update(bean);
						}
						dispose();
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				cancelButton = new JButton("Cancel");
				cancelButton.addActionListener(new ActionListener() {
					
					public void actionPerformed(ActionEvent e) {

						dispose();
					}
				});
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
		getContentPane().setFocusTraversalPolicy(
				new FocusTraversalOnArray(new Component[] { nameField,
						contentPanel, lblName, lblUrl, urlField, buttonPane,
						okButton, cancelButton }));
		setFocusTraversalPolicy(new FocusTraversalOnArray(new Component[] {
				nameField, urlField, okButton, cancelButton }));
		
		// edit mode
		if (bean != null){
			nameField.setText(bean.getName());
			urlField.setText(bean.getUrl());
		}
	}
	
}
